

package com.example.attendancesystem.Listen;


import com.example.attendancesystem.Handler.LoginAndRegisterController;
import com.example.attendancesystem.Models.Admin;
import com.example.attendancesystem.Models.Employee;
import com.example.attendancesystem.Service.LoginAndRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@Component
public class MyHttpSessionListen implements HttpSessionListener {
    @Autowired
    private LoginAndRegisterService loginAndRegisterService;

    @Autowired
    private LoginAndRegisterController loginAndRegisterController;

    public void sessionCreated(HttpSessionEvent se) {
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        String userNumber;
        Employee employee = (Employee) session.getAttribute("emp");
        if (null == employee) {
            Admin admin = (Admin) session.getAttribute("admin");
            if (null != admin) {
                userNumber = admin.getAdminNumber();
                loginAndRegisterService.moveNumberCount(userNumber, session);
                loginAndRegisterController.logout(session);
            }
        } else {
            userNumber = employee.getEmpNumber();
            loginAndRegisterService.moveNumberCount(userNumber, session);
            loginAndRegisterController.logout(session);
        }
    }
}


